package cn.tedu.exam1.user.controller;

import cn.tedu.exam1.base.response.JsonResult;
import cn.tedu.exam1.user.pojo.dto.UserLoginParam;
import cn.tedu.exam1.user.pojo.dto.UserQuery;
import cn.tedu.exam1.user.pojo.dto.UserRegParam;
import cn.tedu.exam1.user.pojo.dto.UserSaveParam;
import cn.tedu.exam1.user.pojo.vo.UserVO;
import cn.tedu.exam1.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    UserService userService;
    @PostMapping("login")
    public JsonResult userLogin(UserLoginParam userLoginParam){
        log.debug("用户登录:userLoginParam"+userLoginParam);
        UserVO userVO = userService.login(userLoginParam);
        //返回userVO方便做会话保持,在localstorage中保存当前登录人的信息
        return JsonResult.ok(userVO);
    }

    @PostMapping("reg")
    public JsonResult reg(@Validated UserRegParam userRegParam){
        log.debug("用户注册:userSaveParam"+userRegParam);
        userService.reg(userRegParam);
        return JsonResult.ok();
    }

    @PostMapping("save")
    public JsonResult saveUser(UserSaveParam userSaveParam){
        log.debug("保存用户信息,userSaveParam={}",userSaveParam);
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }

    @PostMapping("updateAvatar")
    public JsonResult updateAvatar(UserSaveParam userSaveParam){
        log.debug("用户头像参数:userSaveParam={}",userSaveParam);
        userService.updateUserAvatar(userSaveParam);
        return JsonResult.ok();
    }
    @GetMapping("select")
    public JsonResult selectUser(UserQuery userQuery){
        log.debug("查询用户:userQuery={}",userQuery);
        List<UserVO> list = userService.selectUser(userQuery);
        return JsonResult.ok(list);
    }

    @GetMapping("selectId")
    public JsonResult selectId(UserQuery userQuery){
        log.debug("查询用户:userQuery",userQuery);
        UserVO userVO = userService.selectId(userQuery.getId());
        return JsonResult.ok(userVO);
    }
    @PostMapping("/deleteUser/{userId}")
    public JsonResult deleteUser(@PathVariable Long userId){
        log.debug("删除用户,id={}",userId);
        userService.deleteUser(userId);
        return JsonResult.ok();
    }


}